package com.sanpu.kxe.business.member.view;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.bean.UserConfigOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.member.adapter.MemberPayTypeAdapter;
import com.sanpu.kxe.business.member.bean.MemberPayTypeOutput;
import com.sanpu.kxe.business.member.contract.MemberCashierContract;
import com.sanpu.kxe.business.member.contract.MemberConfig;
import com.sanpu.kxe.business.member.presenter.MemberCashierPresenter;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.view.NoScrollListView;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;
import com.sanpu.kxe.module.paypwd.popwindow.SelectPopupWindow;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.SystemBarHelper;
import com.sanpu.kxe.utils.ToastUtil;

import java.math.BigDecimal;
import java.util.List;

import butterknife.Bind;

/**
 * Description：新版会员充值支付页
 * Created by 薛勇军 on 2019/3/19.
 */

public class MemberCashierActivity extends BaseTitleActivity implements MemberCashierContract.View {

    @Bind(R.id.headerView)
    RelativeLayout headerView;
    @Bind(R.id.realPriceTv)
    TextView realPriceTv;
    @Bind(R.id.descriptionRtv)
    RoundTextView descriptionRtv;
    @Bind(R.id.payMethodLv)
    NoScrollListView payMethodLv;
    @Bind(R.id.payBtnTv)
    RoundTextView payBtnTv;
    @Bind(R.id.leftIv)
    ImageView leftIv;

    private SelectPopupWindow vMenuWindow;

    private MemberPayTypeAdapter mPayTypeAdapter;

    private MemberCashierContract.Presenter mPresenter;

    private MemberPayTypeOutput mChooseItem;

    private String realPrice;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_member_cashier;
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) headerView.getLayoutParams();
        params.setMargins(0, barHeight, 0, 0);
        headerView.setLayoutParams(params);

        //打开输入密码的对话框
        vMenuWindow = new SelectPopupWindow(this, new SelectPopupWindow.OnPopWindowClickListener() {
            @Override
            public void onPopWindowClickListener(final String psw, boolean complete) {
                if (psw.length() < 6) return;
                mPresenter.upgradeWithAmt(psw);
                vMenuWindow.clearPsw();
            }
        });

        mPayTypeAdapter = new MemberPayTypeAdapter(mContext);
        payMethodLv.setAdapter(mPayTypeAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        realPrice = getIntent().getStringExtra(MemberConfig.INTENT_MEMBER_PRICE);
        if (!TextUtils.isEmpty(realPrice)) {
            realPriceTv.setText(realPrice);
        }
        String title = getIntent().getStringExtra(MemberConfig.INTENT_MEMBER_TITLE);
        if (!TextUtils.isEmpty(title)) {
            descriptionRtv.setText("会员充值 (" + title + ")");
        }
        String vipOpenId = getIntent().getStringExtra(MemberConfig.INTENT_MEMBER_ID);
        mPresenter = new MemberCashierPresenter(this, vipOpenId, realPrice);
        mPresenter.upgradeVipTypeList();
    }

    @Override
    protected void setListener() {
        super.setListener();
        leftIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        payBtnTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.onPayClickListener(mChooseItem);
            }
        });
        payMethodLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mChooseItem = ((MemberPayTypeAdapter)parent.getAdapter()).getItem(position);
                mPayTypeAdapter.setNotifyData(position);
            }
        });
    }

    @Override
    public void onShowPasswordPopup() {
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null && !TextUtils.isEmpty(realPrice)) {
            BigDecimal mBalance = centerOutput.getBalance();
            if (mBalance == null || mBalance.compareTo(new BigDecimal(realPrice)) == -1) {
                ToastUtil.showTextViewPrompt("余额不足，请先充值！");
                return;
            }
        }
        UserConfigOutput configOutput = CacheManage.getInstance().getCache(CacheModel.USER_CONFIG);
        if (configOutput == null || !TextUtils.equals("1", configOutput.getPayPwd())) {
            GlobalDialog dialog = new GlobalDialog(mContext).headerIcon(R.drawable.img_warm_prompt)
                    .title("温馨提示")
                    .content("余额充值会员前必须设置支付密码");
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    IMainProvider mainProvider = PublicProviderUtil.getProvider(IMainProvider.class);
                    if (mainProvider != null) {
                        mainProvider.openSetPayPwdOneActivity(mContext, true);
                    }
                }
            });
            dialog.show();
            return;
        }
        Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int winHeight = getWindow().getDecorView().getHeight();
        vMenuWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, winHeight - rect.bottom);
    }

    @Override
    public void showDataListView(List<MemberPayTypeOutput> payTypeOutputs) {
        if (payTypeOutputs != null && !payTypeOutputs.isEmpty()) {
            mChooseItem = payTypeOutputs.get(0);
            payTypeOutputs.get(0).setChoose(true);
        }
        mPayTypeAdapter.setNotifyData(payTypeOutputs);
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .navigationBarWithKitkatEnable(false)
                .init();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    public void setPresenter(MemberCashierContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
